<?php
/**
 * Created by PhpStorm.
 * User: insgeek
 * Date: 2017/2/21
 * Time: 15:51
 * 适配器模式
 */
namespace DesignPattern\AdapterPattern;

interface MediaPlayer
{
    public function play(string $audioType, string $fileName);
}

interface AdvancedMediaPlayer
{
    public function playVlc(string $fileName);

    public function playMp4(string $fileName);
}

class VlcPlayer implements AdvancedMediaPlayer
{
    public function playVlc(string $fileName)
    {
        // TODO: Implement playVlc() method.
        print_r("Playing vlc file. Name: " . $fileName . PHP_EOL);
    }

    public function playMp4(string $fileName)
    {
        // TODO: Implement playMp4() method.
    }
}

class Mp4Player implements AdvancedMediaPlayer
{
    public function playVlc(string $fileName)
    {
        // TODO: Implement playVlc() method.
    }

    public function playMp4(string $fileName)
    {
        // TODO: Implement playMp4() method.
        print_r("Playing mp4 file. Name: " . $fileName . PHP_EOL);
    }
}

class MediaAdapter implements MediaPlayer
{
    public $advancedMusicPlayer;

    public function __construct(string $audioType)
    {
        if (strcasecmp($audioType, 'vlc') === 0) {
            $this->advancedMusicPlayer = new VlcPlayer();
        } elseif (strcasecmp($audioType, 'mp4') === 0) {
            $this->advancedMusicPlayer = new Mp4Player();
        }
    }

    public function play(string $audioType, string $fileName)
    {
        // TODO: Implement play() method.
        if (strcasecmp($audioType, 'vlc') === 0) {
            $this->advancedMusicPlayer->playVlc($fileName);
        } elseif (strcasecmp($audioType, 'mp4') === 0) {
            $this->advancedMusicPlayer->playMp4($fileName);
        }
    }

}

class AudioPlayer implements MediaPlayer
{
    public $mediaAdapter;

    public function play(string $audioType, string $fileName)
    {
        // TODO: Implement play() method.
        if (strcasecmp($audioType, 'mp3') === 0) {
            print_r("Playing mp3 file. Name" . $fileName . PHP_EOL);
        } elseif (strcasecmp($audioType, 'vlc') === 0 || strcasecmp($audioType, 'mp4') === 0) {
            $this->mediaAdapter = new MediaAdapter($audioType);
            $this->mediaAdapter->play($audioType, $fileName);
        } else {
            print_r("Invalid media {$audioType} format not supported" . PHP_EOL);
        }
    }
}

$audioPlayer = new AudioPlayer();
$audioPlayer->play('mp3', 'some one like you.mp3');
$audioPlayer->play('mp4', 'see you again.mp4');
$audioPlayer->play('ppt', 'test.ppt');